package com.bootdo.common.utils;

import com.jfinal.weixin.sdk.api.ApiConfig;
import com.jfinal.weixin.sdk.api.ApiConfigKit;
import com.jfinal.weixin.sdk.kit.PaymentKit;
import com.jfinal.weixin.sdk.utils.HttpUtils;

import java.util.Map;

/**
 * 微信红包api
 * @author yiz
 */
public class RedPackApi {

	private static String wxappid="wx24dffff92fcc6bad";
	private static String secret="dd19890387d1beb557ff9322c22d6b1f";
	private static String REDIRECT_URI="http://www.51canting.com/call";

	static {
		ApiConfig apiConfig =new ApiConfig();
		apiConfig.setAppId(wxappid);
		apiConfig.setAppSecret(secret);
		//apiConfig.setToken();
		ApiConfigKit.setThreadLocalApiConfig(apiConfig);
	}



	private static String codeUrl="https://open.weixin.qq.com/connect/qrconnect?appid="+wxappid+"&redirect_uri="+REDIRECT_URI+"REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect";
	private static String codeUrl1="https://open.weixin.qq.com/connect/qrconnect?appid=wx24dffff92fcc6bad&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect";

	// 文档地址：https://pay.weixin.qq.com/wiki/doc/api/cash_coupon.php?chapter=13_5#
	private static String sendRedPackUrl = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";

    //授权
	private static String  authUrl="https://api.weixin.qq.com/sns/oauth2/access_token?appid="+wxappid+"&secret="+secret+"&code=CODE&grant_type=authorization_code";

	/**
	 * 发送红包
	 * @param params 请求参数
	 * @param certPath 证书文件目录
	 * @param partner 证书密码
	 * @return {String}
	 */
	public static String sendRedPack(Map<String, String> params, String certPath, String partner) {
		return HttpUtils.postSSL(sendRedPackUrl, PaymentKit.toXml(params), certPath, partner);
	}

	// 文档地址：https://pay.weixin.qq.com/wiki/doc/api/cash_coupon.php?chapter=13_7&index=6
	private static String getHBInfo = "https://api.mch.weixin.qq.com/mmpaymkttransfers/gethbinfo";

	/**
	 * 根据商户订单号查询信息
	 * @param params 请求参数
	 * @param certPath 证书文件目录
	 * @param partner 证书密码
	 * @return {String}
	 */
	public static String getHbInfo(Map<String, String> params, String certPath, String partner) {
		return HttpUtils.postSSL(getHBInfo, PaymentKit.toXml(params), certPath, partner);
	}

	// 裂变红包：https://pay.weixin.qq.com/wiki/doc/api/cash_coupon.php?chapter=16_5
	private static String sendGroupRedPackUrl = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendgroupredpack";

	/**
	 * 发送裂变红包
	 * @param params 请求参数
	 * @param certPath 证书文件目录
	 * @param partner 证书密码
	 * @return {String}
	 */
	public static String sendGroupRedPack(Map<String, String> params, String certPath, String partner) {
		return HttpUtils.postSSL(sendGroupRedPackUrl, PaymentKit.toXml(params), certPath, partner);
	}



	/**
	 * 获取 accessToken
	 * @return {String}
	 */
	public static String getAuthCode() {
		return HttpUtils.get(authUrl.replace("CODE",getCode()));
	}

	/**
	 * 获取 code
	 * @return {String}
	 */
	public static String getCode() {
		return HttpUtils.get(codeUrl);
	}


	public static void main(String [] ags){

		System.out.println(getCode());

	}

}
